/********************************************************************
Name: Precinct Analyses (Table 2, Figure 3, Table 3, Table 4, Figure A7, Table A4) 
Author: Dan Thompson
Date: September 2021
Description: 

********************************************************************/

clear all
set matsize 11000
set maxvar 15000
gl path = "~/Dropbox/Mariel Effects"


// Table 2

* Prep the data for columns 1 through 3
use "$path/Replication/data/neighborhood_analysis.dta", clear
reshape long share_rep, i(neighborhood) j(year)
gen post = year==80
gen cuban_post = share_cuban*post
egen id = group(neighborhood)

* Table 2, Column 1
areg share_rep cuban_post post, absorb(id) vce(boot, r(1000) cl(id))
local b_cuban1 = _b[cuban_post]
local se_cuban1 = _se[cuban_post]
local n_miami1 = e(N_clust)

* Table 2, Column 2
areg share_rep cuban_post post i.post#c.share_black, absorb(id) vce(boot, r(1000) cl(id))
local b_cuban2 = _b[cuban_post]
local se_cuban2 = _se[cuban_post]
local n_miami2 = e(N_clust)

* Table 2, Column 3
areg share_rep cuban_post post i.post#c.share_white, absorb(id) vce(boot, r(1000) cl(id))
local b_cuban3 = _b[cuban_post]
local se_cuban3 = _se[cuban_post]
local n_miami3 = e(N_clust)

* Prep the data for column 4
use "$path/Replication/data/hisp_precinct_analysis.dta", clear
reshape long share_rep, i(precinct) j(year)
gen hisp_post = reg_hisp_share*(year==80)
gen post = year==80
areg share_rep hisp_post post, absorb(precinct) vce(boot, r(1000) cl(precinct))
local b_hisp4 = _b[hisp_post]
local se_hisp4 = _se[hisp_post]
local n_miami4 = e(N_clust)

* Build Table 2
quietly {
	cap log close
	log using "$path/Replication/output/miami_precincts.tex", text replace
	noisily dis "\begin{table}[t]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{Large Shift toward Republicans among Cubans "
	noisily dis "in Miami.}\label{tab:miami_diff_by_cuban_pop}}"
	noisily dis "\begin{tabular}{lcccc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{4}{c}{Rep Vote [0-1]} \\[1mm]"
	noisily dis "\midrule"
	noisily dis "Cuban Share $ \times $ 1980 & " %3.2f `b_cuban1' " & " %3.2f `b_cuban2' " & " %3.2f `b_cuban3' " & \\"
	noisily dis " & (" %3.2f `se_cuban1' ") & (" %3.2f `se_cuban2' ") & (" %3.2f `se_cuban3' ") & \\[2mm]"
	noisily dis "Hisp Reg Share $ \times $ 1980 & & & & " %3.2f `b_hisp4' " \\"
	noisily dis " & & & & (" %3.2f `se_hisp4' ") \\[2mm]"
	noisily dis "Neighborhood FE & Yes & Yes & Yes & No \\"
	noisily dis "Precinct FE & No & No & No & Yes \\"
	noisily dis "Election FE & Yes & Yes & Yes & Yes \\"
	noisily dis "Black Share $ \times $ 1980 & No & Yes & No & No \\"
	noisily dis "White Share $ \times $ 1980 & No & No & Yes & No \\"
	noisily dis "Neighborhoods & " %3.0f `n_miami1' " & " %3.0f `n_miami2' " & " %3.0f `n_miami3' " & \\"
	noisily dis "Precincts & & & & " %3.0f `n_miami4' " \\"
	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{5}{p{.58\textwidth}}{\footnotesize "
	noisily dis "Block bootstrapped standard errors from 1,000 samples are reported "
	noisily dis "in parentheses below each estimate. "
	noisily dis "All population share variables, including the Cuban population "
	noisily dis "share and the share of registered voters who are Hispanic, range from zero to one.}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	log off
}


// Figure 3

* Plot 1976 to 1980 change in pres election results by neighborhood (left panel)
use "$path/Replication/data/neighborhood_analysis.dta", clear
gen per_cuban = share_cuban*100
replace diff = diff*100
twoway (scatter diff per_cuban, msi(small)) ///
	(lfit diff per_cuban, lc(navy)), ///
	legend(off) ///
	ysc(r(-5 25)) ylab(-5(5)25) ///
	xti("Cuban Population (%)", si(medlarge)) ///
	yti("Change in Rep Vote, 1976 to 1980 (%)", si(medlarge)) ///
	graphregion(color(white)) ///
	name("miami_diff_by_cuban", replace)
graph export "$path/Replication/output/cuban_neighborhoods.pdf", ///
	replace name("miami_diff_by_cuban")

* Plot 1976 to 1980 change in pres election results by precinct (right panel)
use "$path/Replication/data/hisp_precinct_analysis.dta", clear
gen reg_hisp_percent = reg_hisp_share*100
replace diff = diff*100
twoway (scatter diff reg_hisp_percent, msi(small)) ///
	(lfit diff reg_hisp_percent, lc(navy)), ///
	legend(off) ///
	ysc(r(-5 25)) ylab(-5(5)25) ///
	xti("Hispanic Registration Share (%)", si(medlarge)) ///
	graphregion(color(white)) ///
	name("miami_diff_by_hisp", replace)
graph export "$path/Replication/output/hisp_precincts.pdf", ///
	replace name("miami_diff_by_hisp")


// Table 3

* Prepare the data for columns 1-3
use "$path/Replication/data/neighborhood_analysis.dta", clear
append using "$path/Replication/data/hudson_city_analysis.dta"
keep place share_rep76 share_rep80 share_cuban share_black share_white city neighborhood miami
gen id = _n
reshape long share_rep, i(id) j(year)
gen cuban80 = share_cuban*(year==80)
gen miami_cuban80 = cuban80*miami
gen miami80 = miami*(year==80)
gen black80 = share_black*(year==80)
gen miami_black80 = black80*miami
gen white80 = share_white*(year==80)
gen miami_white80 = white80*miami

* Column 1
areg share_rep miami_cuban80 cuban80 miami80 i.year, absorb(id) vce(boot, r(1000) cl(id))
local b_miami1 = _b[miami_cuban80]
local se_miami1 = _se[miami_cuban80]
local b_cuban1 = _b[cuban80]
local se_cuban1 = _se[cuban80]

* Column 2
areg share_rep miami_cuban80 cuban80 miami80 black80 miami_black80 i.year, absorb(id) vce(boot, r(1000) cl(id))
local b_miami2 = _b[miami_cuban80]
local se_miami2 = _se[miami_cuban80]
local b_cuban2 = _b[cuban80]
local se_cuban2 = _se[cuban80]

* Column 3
areg share_rep miami_cuban80 cuban80 miami80 white80 miami_white80 i.year, absorb(id) vce(boot, r(1000) cl(id))
local b_miami3 = _b[miami_cuban80]
local se_miami3 = _se[miami_cuban80]
local b_cuban3 = _b[cuban80]
local se_cuban3 = _se[cuban80]

* Count the number of observations for columns 1 - 3
distinct id if miami==0
local n_hudson1_3 = r(ndistinct)
distinct id if miami==1
local n_miami1_3 = r(ndistinct)

* Prepare the data for columns 4-6
use "$path/Replication/data/neighborhood_analysis.dta", clear
append using "$path/Replication/data/hudson_ward_analysis.dta"
keep place share_rep76 share_rep80 share_cuban share_black share_white ward neighborhood miami
gen id = _n
reshape long share_rep, i(id) j(year)
gen cuban80 = share_cuban*(year==80)
gen miami_cuban80 = cuban80*miami
gen miami80 = miami*(year==80)
gen black80 = share_black*(year==80)
gen miami_black80 = black80*miami
gen white80 = share_white*(year==80)
gen miami_white80 = white80*miami

* Column 4
areg share_rep miami_cuban80 cuban80 miami80 i.year, absorb(id) vce(boot, r(1000) cl(id))
local b_miami4 = _b[miami_cuban80]
local se_miami4 = _se[miami_cuban80]
local b_cuban4 = _b[cuban80]
local se_cuban4 = _se[cuban80]

* Column 5
areg share_rep miami_cuban80 cuban80 miami80 black80 miami_black80 i.year, absorb(id) vce(boot, r(1000) cl(id))
local b_miami5 = _b[miami_cuban80]
local se_miami5 = _se[miami_cuban80]
local b_cuban5 = _b[cuban80]
local se_cuban5 = _se[cuban80]

* Column 6
areg share_rep miami_cuban80 cuban80 miami80 white80 miami_white80 i.year, absorb(id) vce(boot, r(1000) cl(id))
local b_miami6 = _b[miami_cuban80]
local se_miami6 = _se[miami_cuban80]
local b_cuban6 = _b[cuban80]
local se_cuban6 = _se[cuban80]

* Count the number of observations for columns 4 - 6
distinct id if miami==0
local n_hudson4_6 = r(ndistinct)
distinct id if miami==1
local n_miami4_6 = r(ndistinct)


* Build Table 3
quietly {
	cap log close
	log using "$path/Replication/output/miami_vs_hudson.tex", text replace
	noi di "\begin{table}[t]"
	noi di "\centering"
	noi di "\caption{\textbf{Similar Shift toward Republicans among Cubans "
	noi di "in Miami and Hudson County.}\label{tab:miami_vs_hudson}}"
	noi di "\begin{tabular}{lccc|ccc}"
	noi di "\toprule \toprule"
	noi di " & \multicolumn{6}{c}{Rep Vote [0-1]} \\"
	noi di " & \multicolumn{3}{c}{Cities} & \multicolumn{3}{c}{Wards} \\[1mm]"
	noi di "\midrule"
	noi di "Miami $ \times $ Cuban Share $ \times $ 1980 " 
	noi di " & " %3.2f `b_miami1' " & " %3.2f `b_miami2' " "
	noi di " & " %3.2f `b_miami3' " & " %3.2f `b_miami4' " "
	noi di " & " %3.2f `b_miami5' " & " %3.2f `b_miami6' " \\"
	noi di " & (" %3.2f `se_miami1' ") & (" %3.2f `se_miami2' ") "
	noi di " & (" %3.2f `se_miami3' ") & (" %3.2f `se_miami4' ") "
	noi di " & (" %3.2f `se_miami5' ") & (" %3.2f `se_miami6' ") \\[2mm]"
	noi di "Cuban Share $ \times $ 1980 " 
	noi di " & " %3.2f `b_cuban1' " & " %3.2f `b_cuban2' " "
	noi di " & " %3.2f `b_cuban3' " & " %3.2f `b_cuban4' " "
	noi di " & " %3.2f `b_cuban5' " & " %3.2f `b_cuban6' " \\"
	noi di " & (" %3.2f `se_cuban1' ") & (" %3.2f `se_cuban2' ") "
	noi di " & (" %3.2f `se_cuban3' ") & (" %3.2f `se_cuban4' ") "
	noi di " & (" %3.2f `se_cuban5' ") & (" %3.2f `se_cuban6' ") \\[2mm]"
	noi di "Neighborhood FE & Yes & Yes & Yes & Yes & Yes & Yes \\"
	noi di "Election FE & Yes & Yes & Yes & Yes & Yes & Yes \\"
	noi di "Black Share Adjustment & No & Yes & No & No & Yes & No \\"
	noi di "White Share Adjustment & No & No & Yes & No & No & Yes \\"
	noi di "Miami Obs & " %3.0f `n_miami1_3' " & " %3.0f `n_miami1_3' " "
	noi di " & " %3.0f `n_miami1_3' " & " %3.0f `n_miami4_6' " "
	noi di " & " %3.0f `n_miami4_6' " & " %3.0f `n_miami4_6' " \\"
	noi di "Hudson County Obs & " %3.0f `n_hudson1_3' " & " %3.0f `n_hudson1_3' " "
	noi di " & " %3.0f `n_hudson1_3' " & " %3.0f `n_hudson4_6' " "
	noi di " & " %3.0f `n_hudson4_6' " & " %3.0f `n_hudson4_6' " \\[2mm]"
	noi di "\bottomrule \bottomrule"
	noi di "\multicolumn{7}{p{.8\textwidth}}{\footnotesize "
	noi di "Adjustments for particular subpopulations is done by including "
	noi di "two additional vairables in the regression: an interaction between "
	noi di "the subgroup's population share with a flag indicating that the year "
	noi di "is 1980 and separately the subgroup's population share interacted with "
	noi di "a flag for 1980 and Miami. "
	noi di "Block bootstrapped standard errors from 1,000 samples are reported "
	noi di "in parentheses below each estimate. "
	noi di "All population share variables, including the Cuban population "
	noi di "share, range from zero to one.}"
	noi di "\end{tabular}"
	noi di "\end{table}"
	log off
}


// Table 4

* Set up the data for the US House neighborhood-level analysis
use "$path/Replication/data/house_neighborhood_analysis.dta", clear
gen id = _n
egen neighborhood_id = group(neighborhood)
reshape long share_rep, i(id) j(year)
gen cuban80 = share_cuban*(year==80)
gen black80 = per_black*(year==80)
gen white80 = per_white*(year==80)
gen post = year==80

* Column 1
areg share_rep cuban80 post, absorb(id) vce(boot, r(1000) cl(neighborhood_id))
local b_cuban1 = _b[cuban80]
local se_cuban1 = _se[cuban80]
local n_miami1 = e(N_clust)

* Column 2
areg share_rep cuban80 post i.post#i.dist, absorb(id) vce(boot, r(1000) cl(neighborhood_id))
local b_cuban2 = _b[cuban80]
local se_cuban2 = _se[cuban80]
local n_miami2 = e(N_clust)

* Column 3
areg share_rep cuban80 post i.post#i.dist black80, absorb(id) vce(boot, r(1000) cl(neighborhood_id))
local b_cuban3 = _b[cuban80]
local se_cuban3 = _se[cuban80]
local n_miami3 = e(N_clust)

* Column 4
areg share_rep cuban80 post i.post#i.dist white80, absorb(id) vce(boot, r(1000) cl(neighborhood_id))
local b_cuban4 = _b[cuban80]
local se_cuban4 = _se[cuban80]
local n_miami4 = e(N_clust)

* Build Table 4
quietly {
	cap log close
	log using "$path/Replication/output/miami_house.tex", text replace
	noisily dis "\begin{table}[t]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{No Shift toward Republican House Candidates among Cubans "
	noisily dis "in Miami.}\label{tab:miami_house_diff_by_cuban_pop}}"
	noisily dis "\begin{tabular}{lcccc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{4}{c}{Rep Vote [0-1]} \\[1mm]"
	noisily dis "\midrule"
	noisily dis "Cuban Share $ \times $ 1980 & " %3.2f `b_cuban1' " & " %3.2f `b_cuban2' " & " %3.2f `b_cuban3' " & " %3.2f `b_cuban4' " \\"
	noisily dis " & (" %3.2f `se_cuban1' ") & (" %3.2f `se_cuban2' ") & (" %3.2f `se_cuban3' ") & (" %3.2f `se_cuban4' ") \\[2mm]"
	noisily dis "Neighborhood $ \times $ District FE & Yes & Yes & Yes & Yes \\"
	noisily dis "Election FE & Yes & No & No & No \\"
	noisily dis "Election $ \times $ District FE & No & Yes & Yes & Yes \\"
	noisily dis "Black Share $ \times $ 1980 & No & No & Yes & No \\"
	noisily dis "White Share $ \times $ 1980 & No & No & No & Yes \\"
	noisily dis "Neighborhoods & " %3.0f `n_miami1' " & " %3.0f `n_miami2' " & " %3.0f `n_miami3' " & " %3.0f `n_miami4' " \\"
	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{5}{p{.6\textwidth}}{\footnotesize "
	noisily dis "Block bootstrapped standard errors from 1,000 samples are reported "
	noisily dis "in parentheses below each estimate. "
	noisily dis "All population share variables, including the Cuban population "
	noisily dis "share, range from zero to one.}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	log off
}



// Figure A7

use "$path/Replication/data/neighborhood_analysis.dta", clear
append using "$path/Replication/data/hudson_city_analysis.dta"
gen per_diff = diff*100
gen per_cuban = share_cuban*100
twoway (scatter per_diff per_cuban if place=="miami", msi(small)) ///
	(scatter per_diff per_cuban if place=="hudson", msi(small)) ///
	(lfit per_diff per_cuban if place=="miami", lc(navy)) ///
	(lfit per_diff per_cuban if place=="hudson", lc(maroon)), ///
	legend(order(1 "Miami" 2 "Hudson County")) ///
	xti("Cuban Population (%)", si(medlarge)) ///
	yti("Change in Rep Vote, 1976 to 1980 (%)", si(medlarge)) ///
	graphregion(color(white)) ///
	name("miami_vs_hudson", replace)
graph export "$path/Replication/output/miami_vs_hudson.pdf", replace name("miami_vs_hudson")



// Table A4

* Set up the presidential v house precinct analysis file
use "$path/Replication/data/pres_house_analysis.dta", clear
egen neighborhood_id = group(neighborhood)
egen office_neighborhood_id = group(pres neighborhood)
egen election_neighborhood_id = group(year neighborhood)
egen election_office_id = group(year pres)
gen cuban80 = share_cuban*(year==80)
gen pres_cuban80 = pres*cuban80
gen post = year==80

* Column 1
areg share_rep cuban80 pres_cuban80 i.post##i.pres, ///
	absorb(office_neighborhood_id) vce(boot, r(1000) cl(neighborhood_id))
local b11 = _b[pres_cuban80]
local se11 = _se[pres_cuban80]
local b21 = _b[cuban80]
local se21 = _se[cuban80]
local n1 = e(N_clust)

* Column 2
areg share_rep pres_cuban80 i.post#i.neighborhood_id, ///
	absorb(office_neighborhood_id) vce(cluster neighborhood_id)
local b12 = _b[pres_cuban80]
local se12 = _se[pres_cuban80]
local n2 = e(N_clust)


* Build the table
quietly {
	cap log close
	log using "$path/Replication/output/house_v_pres.tex", text replace
	noisily dis "\begin{table}[t]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{No Shift toward Republican House Candidates vs Presidential Candidates among Cubans "
	noisily dis "in Miami.}\label{tab:miami_house_pres_diff_by_cuban_pop}}"
	noisily dis "\begin{tabular}{lcc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{2}{c}{Rep Vote [0-1]} \\[1mm]"
	noisily dis "\midrule"
	noisily dis "Cuban Share $ \times $ Presidential $ \times $ 1980 & " %3.2f `b11' " & " %3.2f `b12' " \\"
	noisily dis " & (" %3.2f `se11' ") & (" %3.2f `se12' ") \\[2mm]"
	noisily dis "Cuban Share $ \times $ 1980 & " %3.2f `b21' " & \\"
	noisily dis " & (" %3.2f `se21' ") & \\[2mm]"
	noisily dis "Neighborhood $ \times $ Office FE & Yes & Yes \\"
	noisily dis "Election $ \times $ Office FE & Yes & No \\"
	noisily dis "Election $ \times $ Neighborhood FE & No & Yes \\"
	noisily dis "Neighborhoods & " %3.0f `n1' " & " %3.0f `n2' " \\"
	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{3}{p{.6\textwidth}}{\footnotesize "
	noisily dis "Block bootstrapped standard errors from 1,000 samples are reported "
	noisily dis "in parentheses below each estimate. "
	noisily dis "All population share variables, including the Cuban population "
	noisily dis "share, range from zero to one.}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	log off
}
